﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;
using Microsoft.AspNetCore.Http;
using AliyunOss;
using TencentCloud.Mongodb.V20190725.Models;

namespace AdminUI.Pages.Mall
{
    public partial class GoodsAddModel : BaseModel
    {
        readonly AliyunOssClient oss;
        public GoodsAddModel(AliyunOssClient ossClient)
        {
            oss = ossClient;
        }

        public void OnGet()
        {
            var list = CategoryBO.Instance.GetList(OnlineTID);
            list.Add(new CategoryDto { CategoryName = "== 请选择商品类别 ==", CategoryID = 0 });
            ViewBag.Categories = list;
        }

        public IActionResult OnPostRBL_SelectedIndexChanged(string selected)
        {
            var flag = selected == "true" ? true : false;
            UIHelper.NumberBox("MAPrice").Enabled(flag);
            UIHelper.NumberBox("MBPrice").Enabled(flag);
            UIHelper.NumberBox("MCPrice").Enabled(flag);
            UIHelper.NumberBox("Commission").Enabled(flag);

            return UIHelper.Result();
        }

        public IActionResult OnPostBtnSubmit_Click(GoodsAddDto dto, IFormFile imageUrl, int[] SalesRange)
        {
            try
            {
                dto.TID = OnlineTID;
                dto.SalesRange = SalesRange.Sum();
                if (imageUrl != null)
                {
                    var img = UploadImage(oss, imageUrl);
                    if (img != null) dto.ImageUrl = img.OssUrl;
                }

                var id = GoodsBO.Instance.Add(dto);
                LogAction(ActionType.新增, $"新增商品：{id}-{dto.GoodsName}");

                FineUICore.PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                //ActiveWindow.HidePostBack();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }


            return UIHelper.Result();
        }

    }
}